package com.beemans.topon.demo.ui.fragments

import android.util.Log
import com.anythink.core.api.ATAdInfo
import com.anythink.core.api.AdError
import com.beemans.common.ui.fragments.CommonFragment
import com.beemans.common.utils.ToastUtils
import com.beemans.topon.ads2.AdvertManager
import com.beemans.topon.ads2.InsertAdvert
import com.beemans.topon.ads2.InsertAdvertCallback
import com.beemans.topon.ads2.InsertAdvertConfig
import com.beemans.topon.demo.R
import com.beemans.topon.demo.constant.Constant
import com.beemans.topon.demo.databinding.FragmentInsertAdBinding
import com.tiamosu.databinding.delegate.lazyDataBindingOrNull
import com.tiamosu.databinding.page.DataBindingConfig
import com.tiamosu.fly.ext.clickNoRepeat

/**
 * @author tiamosu
 * @date 2020/7/8.
 */
class InsertAdFragment : CommonFragment() {
    private val dataBinding: FragmentInsertAdBinding? by lazyDataBindingOrNull { }
//    private val insertAdConfig by lazy {
//        TpInsertAd.TpInsertAdConfig(
//            Constant.INSERT_ID,
//            retryCount = 3
//        )
//    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.fragment_insert_ad)
    }

    override fun initEvent() {
        // 加载广告
        dataBinding?.insertAdBtnPreload?.clickNoRepeat {
            preloadAdvert()

//            val config = insertAdConfig.deepClone()?.apply { isOnlyPreload = true }
//                ?: return@setOnClickListener
//            TpInsertAd.start(this, config) {
//                onAdLoaded {
//                    val cache = TopOn.getInsertAdCaches(this@InsertAdFragment, config.placementId)
//                    Log.e("susu2", "cache:${cache.size}")
//                }
//            }
        }

        // 展示广告
        dataBinding?.insertAdBtnShow?.clickNoRepeat {
            showAdvert()

//            val config = insertAdConfig.deepClone()?.apply { isOnlyPreload = true }
//                ?: return@clickNoRepeat
//            TpInsertAd.start(this, insertAdConfig)
//            TpInsertAd.start(this, config)
//            TpInsertAd.start(this, insertAdConfig)
        }
    }

    /**
     * 加载广告
     */
    private fun preloadAdvert() {
        val advertConfig = InsertAdvertConfig(Constant.INSERT_ID)

        AdvertManager.loadInsertAdvert(this, advertConfig, object : InsertAdvertCallback() {
            override fun onAdvertLoaded(insertAdvert: InsertAdvert) {
                printLog("广告加载成功")
            }

            override fun onAdvertLoadFail(insertAdvert: InsertAdvert, adError: AdError?) {
                printLog("广告加载失败")
            }
        })
    }

    /**
     * 展示广告
     */
    private fun showAdvert() {
        val advertConfig = InsertAdvertConfig(Constant.INSERT_ID)
        advertConfig.isShowAfterLoaded = true
        advertConfig.isPreloadAdvert = true
        advertConfig.isPreloadWithoutReady = true

        AdvertManager.loadInsertAdvert(this, advertConfig, object : InsertAdvertCallback() {
            override fun onAdvertLoaded(insertAdvert: InsertAdvert) {
                printLog("广告加载成功")
            }

            override fun onAdvertLoadFail(insertAdvert: InsertAdvert, adError: AdError?) {
                printLog("广告加载失败")
            }

            override fun onAdvertShow(insertAdvert: InsertAdvert, atAdInfo: ATAdInfo?) {
                printLog("广告展示")
            }

            override fun onAdvertClicked(insertAdvert: InsertAdvert, atAdInfo: ATAdInfo?) {
                printLog("广告点击")
            }

            override fun onAdvertClose(insertAdvert: InsertAdvert, atAdInfo: ATAdInfo?): Boolean {
                printLog("广告关闭")
                return true
            }
        })
    }

    private fun printLog(msg: String) {
        Log.e("susu", msg)
        ToastUtils.showShort(msg)
    }

    override fun doBusiness() {
    }
}